home *** CD-ROM | disk | FTP | other *** search
- /*
- alphaSort(direction,string) -- Sort the lines in string in either ascending (direction = "ascending") or
- descending (direction = "descending") order.
-
- Written By Greg Kimberly
- ©Apple Computer, Inc. 1988
- All Rights Reserved.
-
- To compile and link this file using Macintosh Programmer's Workshop,
-
- c -b alphaSort.c
- link -m ENTRYPOINT -o "TCP XCMD Example" -rt XFCN=7872 -sg alphaSort=Main,%A5Init,STDIO,INTENV ∂
- alphaSort.c.o "{Libraries}HyperXLib.o" "{Libraries}Interface.o" "{CLibraries}CInterface.o" ∂
- "{CLibraries}StdCLib.o"
-
- Initial coding 1988 by Greg Kimberly.
- Modified for use in MacTCP Toolkit 3/27/89 by Harry Chesley.
- */
-
- #include <Memory.h>
- #include <Packages.h>
- #include <Resources.h>
- #include <OSUtils.h>
- #include <HyperXCmd.h>
- #include <stdio.h>
- #include <string.h>
- #include <strings.h>
-
- pascal void EntryPoint(XCmdPtr paramPtr)
-
- {
- Ptr oldA5, myA5;
- Ptr A5Alloc();
- void A5Init(Ptr myA5);
- Ptr A5Swap (Ptr myA5);
- void A5Dispose (Ptr myA5);
-
- /* Allocate and install our global variable space. */
- myA5 = A5Alloc();
- A5Init(myA5);
- oldA5 = A5Swap(myA5);
-
- /* Do the sort. */
- alphaSort(paramPtr);
-
- /* Deinstall and deallocate the globals. */
- A5Swap(oldA5);
- A5Dispose(myA5);
- }
-
- alphaSort(paramPtr)
-
- XCmdPtr paramPtr;
-
- {
- Ptr *pointerArray; /* Array of pointers to line starting points. */
- Handle myListHand; /* Handle to input list. */
- char *myListScan; /* Pointer for scanning the list. */
- Handle returnListHand; /* Handle to the output. */
- char *returnListScan; /* Pointer for scanning through result. */
- char adjective; /* Pointer to type of sort requested string. */
- long countCRs; /* Count of number of lines. */
- long i;
- long (*compareFunc) ();
- long compareAlphaUp();
- long compareAlphaDown();
- void errAbort();
-
- /* Check that we've got the right number of parameters. */
- if (paramPtr->paramCount < 1) {
- errAbort(paramPtr,"§§§ parameter count must be at least 1 §§§");
- return;
- };
-
- /* Get the parameters. */
- myListHand = paramPtr->params[0];
- if (paramPtr->paramCount == 1) adjective = 'a';
- else if (paramPtr->params[1] == NULL) adjective = 'a';
- else adjective = **(paramPtr->params[1]);
-
- /* Decide what type of sort to do. */
- if ((adjective == 'd') || (adjective == 'D')) compareFunc = compareAlphaDown;
- else if ((adjective == 'a') || (adjective == 'A')) compareFunc = compareAlphaUp;
- else {
- errAbort(paramPtr,"§§§ unknown type of sort requested §§§");
- return;
- };
-
- /* Allocate the result. */
- returnListHand = NewHandle((long) 1 + strlen(*myListHand));
- if (returnListHand == NULL) {
- errAbort(paramPtr,"§§§ couldn't allocate result §§§");
- return;
- };
-
- /* Count the number of lines. */
- countCRs = 0;
- myListScan = *myListHand;
- while (*myListScan)
- if (*myListScan++ == '\n') countCRs++;
-
- /* Allocate an array of pointers into the lines. */
- pointerArray = (Ptr *) NewPtr((countCRs + 1) * sizeof(Ptr));
- if (pointerArray == NULL) {
- DisposHandle(returnListHand);
- errAbort(paramPtr,"§§§ out of memory §§§");
- return;
- };
-
- /* Fill in the pointers to the lines of the source text. */
- countCRs = 0;
- pointerArray[countCRs] = *myListHand;
- countCRs = 1;
- myListScan = *myListHand;
- while (*myListScan)
- if (*myListScan++ == '\n') {
- *(myListScan-1) = 0;
- if (*myListScan != 0) /* Avoid '\n' that terminates list. */
- pointerArray[countCRs++] = myListScan;
- };
-
- /* Sort the line pointers. */
- HLock(myListHand);
- qsort(pointerArray,countCRs,sizeof(Ptr),compareFunc);
- HUnlock(myListHand);
-
- /* Transfer the sorted lines into the result handle. */
- returnListScan = *returnListHand;
- /* Note that countCRs isn't zero-based -- have to make up for the extra count. */
- for(i=0;i<countCRs;i++) {
- myListScan = pointerArray[i];
- while (*myListScan != 0) *returnListScan++ = *myListScan++;
- *returnListScan++ = '\n'; /* Insert the '\n'. */
- };
- /* Put a terminator on the string. */
- *(returnListScan-1) = 0;
-
- DisposPtr((Ptr) pointerArray);
- paramPtr->returnValue = returnListHand;
- }
-
- /* Compare routines called by quick sort. */
-
- int compareAlphaDown(el1,el2)
-
- char **el1,**el2;
-
- {
- return(-strcmp(*el1,*el2));
- }
-
- int compareAlphaUp(el1,el2)
-
- char **el1,**el2;
-
- {
- return(strcmp(*el1,*el2));
- }
-
- /* Error handling: return the string as a result. */
-
- void errAbort(paramPtr,str)
-
- XCmdPtr paramPtr;
- char *str;
-
- {
- Handle nuHndl;
-
- /* Allocate space for an error message, copy the string into it, and return the handle to HyperCard. */
- nuHndl = NewHandle((long)(strlen(str)+1));
- if (nuHndl == nil) return;
- strcpy((char *)*nuHndl,str);
- paramPtr->returnValue = nuHndl;
- }
-